/**
 * 案件实体类
 * 定义AI侦探应用中的案件数据结构
 */
export class Case {
  // 案件ID
  id: string;
  
  // 案件标题
  title: string;
  
  // 案件描述
  description: string;
  
  // 案件状态
  status: 'open' | 'investigating' | 'solved' | 'closed';
  
  // 创建者ID
  creatorId: string;
  
  // 创建时间
  createdAt: Date;
  
  // 更新时间
  updatedAt: Date;
  
  // 案件类型
  caseType: string;
  
  // 案件地点
  location: string;
  
  // 案件发生时间
  incidentTime?: Date;
  
  // 相关人员ID列表
  relatedPersonIds: string[];
  
  // 证据ID列表
  evidenceIds: string[];
  
  // 线索ID列表
  clueIds: string[];
  
  // AI分析结果
  aiAnalysis?: {
    summary: string;
    keyFindings: string[];
    recommendations: string[];
    confidenceScore: number;
  };
  
  /**
   * 构造函数
   * @param caseData 案件数据
   */
  constructor(caseData: {
    id?: string;
    title: string;
    description: string;
    status?: 'open' | 'investigating' | 'solved' | 'closed';
    creatorId: string;
    createdAt?: Date;
    updatedAt?: Date;
    caseType: string;
    location: string;
    incidentTime?: Date;
    relatedPersonIds?: string[];
    evidenceIds?: string[];
    clueIds?: string[];
    aiAnalysis?: {
      summary: string;
      keyFindings: string[];
      recommendations: string[];
      confidenceScore: number;
    };
  }) {
    this.id = caseData.id || this.generateId();
    this.title = caseData.title;
    this.description = caseData.description;
    this.status = caseData.status || 'open';
    this.creatorId = caseData.creatorId;
    this.createdAt = caseData.createdAt || new Date();
    this.updatedAt = caseData.updatedAt || new Date();
    this.caseType = caseData.caseType;
    this.location = caseData.location;
    this.incidentTime = caseData.incidentTime;
    this.relatedPersonIds = caseData.relatedPersonIds || [];
    this.evidenceIds = caseData.evidenceIds || [];
    this.clueIds = caseData.clueIds || [];
    this.aiAnalysis = caseData.aiAnalysis;
  }
  
  /**
   * 生成唯一ID
   * @returns 唯一ID字符串
   */
  private generateId(): string {
    return `case_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }
  
  /**
   * 更新案件状态
   * @param status 新状态
   */
  updateStatus(status: 'open' | 'investigating' | 'solved' | 'closed'): void {
    this.status = status;
    this.updatedAt = new Date();
  }
  
  /**
   * 添加相关人员
   * @param personId 人员ID
   */
  addRelatedPerson(personId: string): void {
    if (!this.relatedPersonIds.includes(personId)) {
      this.relatedPersonIds.push(personId);
      this.updatedAt = new Date();
    }
  }
  
  /**
   * 添加证据
   * @param evidenceId 证据ID
   */
  addEvidence(evidenceId: string): void {
    if (!this.evidenceIds.includes(evidenceId)) {
      this.evidenceIds.push(evidenceId);
      this.updatedAt = new Date();
    }
  }
  
  /**
   * 添加线索
   * @param clueId 线索ID
   */
  addClue(clueId: string): void {
    if (!this.clueIds.includes(clueId)) {
      this.clueIds.push(clueId);
      this.updatedAt = new Date();
    }
  }
  
  /**
   * 转换为JSON对象
   * @returns JSON格式的案件数据
   */
  toJSON() {
    return {
      id: this.id,
      title: this.title,
      description: this.description,
      status: this.status,
      creatorId: this.creatorId,
      createdAt: this.createdAt.toISOString(),
      updatedAt: this.updatedAt.toISOString(),
      caseType: this.caseType,
      location: this.location,
      incidentTime: this.incidentTime?.toISOString(),
      relatedPersonIds: this.relatedPersonIds,
      evidenceIds: this.evidenceIds,
      clueIds: this.clueIds,
      aiAnalysis: this.aiAnalysis
    };
  }
  
  /**
   * 从JSON对象创建Case实例
   * @param json JSON格式的案件数据
   * @returns Case实例
   */
  static fromJSON(json: any): Case {
    return new Case({
      id: json.id,
      title: json.title,
      description: json.description,
      status: json.status,
      creatorId: json.creatorId,
      createdAt: json.createdAt ? new Date(json.createdAt) : new Date(),
      updatedAt: json.updatedAt ? new Date(json.updatedAt) : new Date(),
      caseType: json.caseType,
      location: json.location,
      incidentTime: json.incidentTime ? new Date(json.incidentTime) : undefined,
      relatedPersonIds: json.relatedPersonIds || [],
      evidenceIds: json.evidenceIds || [],
      clueIds: json.clueIds || [],
      aiAnalysis: json.aiAnalysis
    });
  }
}